package com.example.common.utils;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.alibaba.fastjson.JSONObject;
import com.example.common.support.ApiException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wxy
 * @Date 2021/1/25 11:51
 * @Version 1.0
 */
@Component
public class HxUtils {

    public static final String ORG_NAME = "1103190516010105";
    public static final String APP_NAME = "hlwyy";
    public static final String CLIENT_ID = "YXA6X5NNwHehEem23bnmoFpUVQ";
    public static final String CLIENT_SECRET = "YXA6exsPCH87JFGj5X1pPpRg0-mccAQ";
    public static final String PRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAPi0zPFsIhNe4hScJ8rp656yJS6s4R7hvtLmpRjvbnX6AFlHq6Es6pJMUGlXOvoCpbPTwGWwxBXlPDVweng3UiHdodtjiVCCd4v9ysvntjuJfnbZZdo23suYeXvir9K2q4S2RF9+tfvsGZzN7L2pGeg/1TKB6oAN8CJuRV0pqBGXAgMBAAECgYEA10fhswTwBiZ2unJlO476ptb7uohpIcs02+hMduYijrrF/II+fXic3+BxJo4cffc2qg+clinJwk44rOVdDZyNWRBdODSBFa3XMXRNlY7OWXIIFwsSe28jqC2jr3DX3ad7w0P2Xx3TcK0BkUVfKwhrfd8xmonjVqUs3RIJUwE/wZkCQQD+7GiVpMaITXFc9Nh9BUOcvC8bRYo4kljWemywvKwRQTnssW939mOM1lBs9gMn4T4r66nL+zjK6sdKh0OJoESNAkEA+cGrtd/691JyMdP9C4mKPgsfprnAHNg1irrokG0LmZ3+vavQz615ee1fALzkZLLl6bGmcUnODabRlscyjhtvswJAICUfav9EKZvaV6ZoRhuL0w/HwOCb9VY+7Yyyd/bSm7pl23rkW3QuSWwc6CB9dWHpvRAScS5UkwHX74Xu9kPHYQJBAKSnn9KJ1WSUACEFhv/CL1dz8evy7cqWZ8YMLyJ4IXunA7h7u3APB9TwPv+3iEMzBgwpSHfLloK9hdOHg7bYM60CQQC5gIyOainD9aRnFZQ+u2fPXJhCEp4Idyu7DD0n0v2AqMeY3TDgbtzq3ar6/Dkzvpfv/R4vnDvL7CLmwMrv7MjD";
    public static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD4tMzxbCITXuIUnCfK6euesiUurOEe4b7S5qUY7251+gBZR6uhLOqSTFBpVzr6AqWz08BlsMQV5Tw1cHp4N1Ih3aHbY4lQgneL/crL57Y7iX522WXaNt7LmHl74q/StquEtkRffrX77Bmczey9qRnoP9UygeqADfAibkVdKagRlwIDAQAB";
    public static final String HX_URL = "https://a1.easemob.com/%s/%s/users";

    @Resource
    private RestTemplate restTemplate;


    public String enCodePwd(String openId){
        RSA rsa = new RSA(PRIVATE_KEY, PUBLIC_KEY);
        byte[] bytes = rsa.encrypt(openId.getBytes(), KeyType.PrivateKey);
        String pwd = Base64.getEncoder().encodeToString(bytes);
        return pwd;
    }

    public boolean createHXID(String openId, String pwd) {
        String url = String.format(HX_URL, ORG_NAME, APP_NAME);

        Map<String, Object> inputMap = new HashMap<>();
        inputMap.put("username", openId);
        inputMap.put("password", pwd);
        inputMap.put("nickname", "");
        String inputJson = JSONObject.toJSONString(inputMap);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(inputJson, headers);

        Map<?, ?> dataMap = restTemplate.postForObject(url, request, Map.class);
        assert dataMap != null;
        Object errorCode = dataMap.get("error");
        if (null != errorCode) {
            String errorDesc = dataMap.get("error_description").toString();
            if (!errorDesc.contains("exists")) {
                throw new ApiException(1, errorDesc);
            }
        }
        return true;
    }
}
